﻿using ICSharpCode.SharpZipLib.Zip;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;

namespace XKPlatform.Common
{
    public class ZipHelper
    {
        /// <summary>
        /// 解压zip文件
        /// </summary>
        /// <param name="stream"></param>
        public static void UnZipFile(Stream stream)
        {
            using (ZipInputStream s = new ZipInputStream(stream))
            {
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string directoryName = Path.GetDirectoryName(theEntry.Name);
                    if (directoryName.Length > 0)
                    {
                        Directory.CreateDirectory(directoryName);
                    }

                    string fileName = Path.GetFileName(theEntry.Name);
                    if (fileName != String.Empty)
                    {
                        using (FileStream streamWriter = File.Create(theEntry.Name))
                        {
                            int size = 2048;
                            byte[] data = new byte[2048];
                            while (true)
                            {
                                size = s.Read(data, 0, data.Length);
                                if (size > 0)
                                {
                                    streamWriter.Write(data, 0, size);
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 解析zip文件并读取csv文件
        /// </summary>
        /// <param name="stream"></param>
        public static List<DataTable> ParseZipFile(Stream stream)
        {
            List<DataTable> list = new List<DataTable>();
            using (ZipInputStream s = new ZipInputStream(stream))
            {
                ZipEntry theEntry;
                while ((theEntry = s.GetNextEntry()) != null)
                {
                    string fileName = Path.GetFileName(theEntry.Name);
                    if (!string.IsNullOrEmpty(fileName) && fileName.ToLower().EndsWith(".csv"))
                    {
                        int size = 2048;
                        byte[] data = new byte[2048];
                        MemoryStream csvStream = new MemoryStream();
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                csvStream.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        //读取csv
                        //var ds = CsvReaderHelper.ReadCsv(csvStream);
                        var dt = CsvReaderHelper.ReadCSV(csvStream.ToArray());
                        list.Add(dt);
                    }
                }

                s.Close();
            }

            return list;
        }
    }
}